package com.hnu.highlynewapp.authentication;

import com.hnu.highlynewapp.entity.ContactEntity;
import com.hnu.highlynewapp.entity.FinancialInstitutionInfoEntity;
import com.hnu.highlynewapp.enums.StatusEnum;
import com.hnu.highlynewapp.exception.BizCodeEnum;
import com.hnu.highlynewapp.service.ContactService;
import com.hnu.highlynewapp.service.FinancialInstitutionInfoService;
import com.hnu.highlynewapp.utils.R;
import com.hnu.highlynewapp.vo.FinancialInstitutionInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("金融机构认证api")
@RestController
@RequestMapping("/financialinstitutioninfoauthentication")
public class FinancialInstitutionInfoAuthenticationController {
    @Autowired
    FinancialInstitutionInfoService financialInstitutionInfoService;
    @Autowired
    ContactService contactService;

    @ApiOperation("保存金融机构证信息")
    @PostMapping(value = "/save")
    //@RequiresPermissions("highlynewapp:businessinfo:save")
    public R save(@Validated @RequestBody FinancialInstitutionInfoVo financialInstitutionInfoVo, BindingResult result) {
        Map<String, String> map = new HashMap<>();

        if (result.hasErrors()) {
            //获取效验错误结果
            result.getFieldErrors().forEach((item) -> {
                //获取到错误提示
                String message = item.getDefaultMessage();
                //获取错误的属性的名字
                String field = item.getField();
                map.put(field, message);
            });
            return R.error(BizCodeEnum.INPUT_INVALID.getCode(), BizCodeEnum.INPUT_INVALID.getMessage()).put("data", map);
        } else {
            financialInstitutionInfoService.auth(financialInstitutionInfoVo);
            return R.ok();
        }
    }

    @ApiOperation("显示所有还未认证的金融机构信息")
    @PostMapping(value = "/nocheck")
    public R nocheck() {
        List<FinancialInstitutionInfoEntity> financialInstitutionInfoEntityList = financialInstitutionInfoService.getNoCheckList();

        List<FinancialInstitutionInfoVo> financialInstitutionInfoVoList = new ArrayList<FinancialInstitutionInfoVo>() {//这个大括号 就相当于我们  new 接口
            {//这个大括号 就是 构造代码块 会在构造函数前 调用


                int i;

                for (i = 0; i < financialInstitutionInfoEntityList.size(); i++) {
                    FinancialInstitutionInfoVo financialInstitutionInfoVo = new FinancialInstitutionInfoVo();
                    ContactEntity contactEntity = contactService.getById(financialInstitutionInfoEntityList.get(i).getContactId());
                    System.out.println(contactEntity);
                    BeanUtils.copyProperties(financialInstitutionInfoEntityList.get(i), financialInstitutionInfoVo);

                    financialInstitutionInfoVo.setContactAddress(contactEntity.getAddress());
                    financialInstitutionInfoVo.setContactEmail(contactEntity.getEmail());
                    financialInstitutionInfoVo.setContactName(contactEntity.getName());

                    financialInstitutionInfoVo.setContactPhone(contactEntity.getPhone());
                    financialInstitutionInfoVo.setContactPosition(contactEntity.getPosition());
                    add(financialInstitutionInfoVo);
                }

            }
        };
        return R.ok().put("data", financialInstitutionInfoEntityList);
    }

    @ApiOperation("通过financialInstitutionId来通过认证")
    @PostMapping("/passcheck/{financialInstitutionId}")
    public R passcheck(@PathVariable("financialInstitutionId") Long financialInstitutionId) {
        financialInstitutionInfoService.passCheck(financialInstitutionId);

        return R.ok();
    }

    @ApiOperation("通过financialInstitutionId来拒绝通过认证")
    @PostMapping("/failcheck/{financialInstitutionId}")
    public R failcheck(@PathVariable("financialInstitutionId") Long financialInstitutionId) {
        financialInstitutionInfoService.failCheck(financialInstitutionId);

        return R.ok();
    }
}


